use serde::{Serialize, Deserialize, Deserializer};
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;

use mongodb::{
    bson::{doc, Document},
    sync::Client,
};
use walkdir::WalkDir;

// 诗歌实体
#[derive(Serialize, Deserialize, Debug)]
struct Poet {
    title: String,
    paragraphs: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug)]
struct PoetStrains {
    id: String,
    strains: Vec<String>,
}

// 唐宋诗歌实体
#[derive(Serialize, Deserialize, Debug)]
struct PoetDesc {
    id: String,
    title: String,
    author: String,
    paragraphs: Vec<String>,
}

// 楚辞实体
#[derive(Serialize, Deserialize, Debug)]
struct ChuCi {
    title: String,
    section: String,
    author: String,
    content: Vec<String>
}

// 宋词作者实体
#[derive(Serialize, Deserialize, Debug, Clone)]
struct SongCiAuthor {
    name: String,
    #[serde(deserialize_with="parse_null")]
    short_description: String, // 存在 null 字符串时，通过属性定义方法，处理特殊字符
    #[serde(deserialize_with="parse_null")]
    description: String
}

// 宋词实体
#[derive(Serialize, Deserialize, Debug, Clone)]
struct SongCi {
    rhythmic: String,
    author: String,
    paragraphs: Vec<String>
}

// 诗作者实体
#[derive(Serialize, Deserialize, Debug, Clone)]
struct PoetAuthor {
    id: String,
    name: String,
    desc: String
}

// 论语实体
#[derive(Serialize, Deserialize, Debug, Clone)]
struct Lunyu {
    chapter: String,
    paragraphs: Vec<String>
}

// 百家姓来源 实体
#[derive(Serialize, Deserialize, Debug, Clone)]
struct Origin {
    surname: String,
    place: String,
}

// 百家姓实体
#[derive(Serialize, Deserialize, Debug, Clone)]
struct Baijiaxing {
    title: String,
    author: String,
    tags: String,
    paragraphs: Vec<String>,
    origin: Vec<Origin>
}

// 弟子规实体
#[derive(Serialize, Deserialize, Debug, Clone)]
struct DiZiGui {
    title: String,
    author: String,
    content: Vec<Lunyu>,//因为和论语的实体字段一样，就直接引用了
}
// 古文观止 卷内容实体
#[derive(Serialize, Deserialize, Debug, Clone)]
struct JuanDesc {
    chapter: String,
    source: String,
    author: String,
    paragraphs: Vec<String>,
}
// 古文观止 卷实体
#[derive(Serialize, Deserialize, Debug, Clone)]
struct Juan {
    title: String,
    content: Vec<JuanDesc>,
}
// 古文观止实体
#[derive(Serialize, Deserialize, Debug, Clone)]
struct GuWenGuanZhi {
    title: String,
    brief: Vec<String>,
    content: Vec<Juan>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
struct QianSubDesc {
    subchapter: String,
    paragraphs: Vec<String>,
}
// 千家诗 诗歌 实体
#[derive(Serialize, Deserialize, Debug, Clone)]
struct QianDesc {
    chapter: String,
    author: String,
    #[serde(default = "default_qian_desc")]
    paragraphs: Vec<String>,
    #[serde(default = "default_qian_sub_desc")]
    subparagraphs: Vec<QianSubDesc>,
}
// 千家诗 类型 实体
#[derive(Serialize, Deserialize, Debug, Clone)]
struct PoetType {
    poet_type: String,
    content: Vec<QianDesc>,
}
// 千家诗实体
#[derive(Serialize, Deserialize, Debug, Clone)]
struct QianJiaShi {
    title: String,
    author: String,
    content: Vec<PoetType>,
}

// 千字文 实体
#[derive(Serialize, Deserialize, Debug, Clone)]
struct QianZiWen {
    title: String,
    author: String,
    tags: String,
    paragraphs: Vec<String>,
    spells: Vec<String>,
}
// 三字经 实体
#[derive(Serialize, Deserialize, Debug, Clone)]
struct SanZiJing {
    title: String,
    author: String,
    tags: String,
    paragraphs: Vec<String>
}
// 声律启蒙 卷 卷内容
#[derive(Serialize, Deserialize, Debug, Clone)]
struct ShengLvQinMengJuanDesc {
    chapter: String,
    paragraphs: Vec<String>
}
// 声律启蒙 卷
#[derive(Serialize, Deserialize, Debug, Clone)]
struct ShengLvQinMengJuan {
    title: String,
    content: Vec<ShengLvQinMengJuanDesc>
}
// 声律启蒙
#[derive(Serialize, Deserialize, Debug, Clone)]
struct ShengLvQinMeng {
    title: String,
    author: String,
    brief: String,
    content: Vec<ShengLvQinMengJuan>
}

// 唐诗三百首 诗体
#[derive(Serialize, Deserialize, Debug, Clone)]
struct TangShiSanBaiShouDesc {
    chapter: String,
    #[serde(deserialize_with="parse_null")]
    subchapter: String,
    author: String,
    paragraphs: Vec<String>
}
// 唐诗三百首 类型
#[derive(Serialize, Deserialize, Debug, Clone)]
struct TangShiSanBaiShouType {
    poet_type: String,
    content: Vec<TangShiSanBaiShouDesc>
}
// 唐诗三百首
#[derive(Serialize, Deserialize, Debug, Clone)]
struct TangShiSanBaiShou {
    title: String,
    content: Vec<TangShiSanBaiShouType>
}

// 文字蒙求 卷
#[derive(Serialize, Deserialize, Debug, Clone)]
struct WenZiMengQiuJuan {
    title: String,
    paragraphs: Vec<String>,
}
// 文字蒙求
#[derive(Serialize, Deserialize, Debug, Clone)]
struct WenZiMengQiu {
    title: String,
    author: String,
    brief: String,
    preface: Vec<String>,
    content: Vec<WenZiMengQiuJuan>
}

// 幼学琼林 正文
#[derive(Serialize, Deserialize, Debug, Clone)]
struct YouXueQiongLinDesc {
    chapter: String,
    paragraphs: Vec<String>,
}
// 幼学琼林 卷
#[derive(Serialize, Deserialize, Debug, Clone)]
struct YouXueQiongLinJuan {
    title: String,
    content: Vec<YouXueQiongLinDesc>,
}
// 幼学琼林
#[derive(Serialize, Deserialize, Debug, Clone)]
struct YouXueQiongLin {
    title: String,
    author: String,
    brief: String,
    content: Vec<YouXueQiongLinJuan>
}

// 增广贤文 集
#[derive(Serialize, Deserialize, Debug, Clone)]
struct ZengGuangXianWenJi {
    chapter: String,
    paragraphs: Vec<String>,
}
// 增广贤文
#[derive(Serialize, Deserialize, Debug, Clone)]
struct ZengGuangXianWen {
    title: String,
    author: String,
    brief: String,
    content: Vec<ZengGuangXianWenJi>
}

// 朱子家训
#[derive(Serialize, Deserialize, Debug, Clone)]
struct ZhuZiJiaXun {
    title: String,
    author: String,
    paragraphs: Vec<String>
}

// 诗经
#[derive(Serialize, Deserialize, Debug, Clone)]
struct ShiJing {
    title: String,
    chapter: String,
    section: String,
    content: Vec<String>
}
// 大学
#[derive(Serialize, Deserialize, Debug, Clone)]
struct DaXue {
    chapter: String,
    paragraphs: Vec<String>
}

// 花间集
#[derive(Serialize, Deserialize, Debug, Clone)]
struct HuaJiaJi {
    title: String,
    author: String,
    rhythmic: String,
    paragraphs: Vec<String>,
    notes: Vec<String>
}

// 幽梦影
#[derive(Serialize, Deserialize, Debug, Clone)]
struct YouMengYing {
    content: String,
    comment: Vec<String>
}


// 数据空字符处理
fn parse_null<'de, D>(d: D) -> Result<String, D::Error> where D: Deserializer<'de> {
    Deserialize::deserialize(d)
        .map(|x: Option<_>| {
            x.unwrap_or("".to_string())
        })
}

fn default_qian_desc()->Vec<String>{
    Vec::new()
}


fn default_qian_sub_desc()->Vec<QianSubDesc>{
    Vec::new()
}


// 读取曹操诗歌json文件
fn read_user_from_file_poet<P: AsRef<Path>>(path: P) -> Result<Vec<Poet>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取唐宋诗歌 声标json文件
fn read_user_from_file_poet_strains<P: AsRef<Path>>(path: P) -> Result<Vec<PoetStrains>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}

// 读取唐宋诗歌json文件
fn read_user_from_file_poet_desc<P: AsRef<Path>>(path: P) -> Result<Vec<PoetDesc>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取楚辞json文件
fn read_user_from_file_chu_ci<P: AsRef<Path>>(path: P) -> Result<Vec<ChuCi>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取宋词作者json文件
fn read_user_from_file_song_ci_author<P: AsRef<Path>>(path: P) -> Result<Vec<SongCiAuthor>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取宋词json文件
fn read_user_from_file_song_ci<P: AsRef<Path>>(path: P) -> Result<Vec<SongCi>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取唐宋诗作者json文件
fn read_user_from_file_poet_author<P: AsRef<Path>>(path: P) -> Result<Vec<PoetAuthor>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取论语json文件
fn read_user_from_file_lun_yu<P: AsRef<Path>>(path: P) -> Result<Vec<Lunyu>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取百家姓json文件
fn read_user_from_file_bai_jia_xing<P: AsRef<Path>>(path: P) -> Result<Vec<Baijiaxing>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取弟子规json文件
fn read_user_from_file_di_zi_gui<P: AsRef<Path>>(path: P) -> Result<Vec<DiZiGui>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取古文观止json文件
fn read_user_from_file_gu_wen_guan_zhi<P: AsRef<Path>>(path: P) -> Result<Vec<GuWenGuanZhi>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取千家诗json文件
fn read_user_from_file_qian_jia_shi<P: AsRef<Path>>(path: P) -> Result<Vec<QianJiaShi>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取千字文json文件
fn read_user_from_file_qian_zi_wen<P: AsRef<Path>>(path: P) -> Result<Vec<QianZiWen>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取三字经json文件
fn read_user_from_file_san_zi_jing<P: AsRef<Path>>(path: P) -> Result<Vec<SanZiJing>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取声律启蒙json文件
fn read_user_from_file_sheng_lv_qi_meng<P: AsRef<Path>>(path: P) -> Result<Vec<ShengLvQinMeng>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取唐诗三百首json文件
fn read_user_from_file_tang_shi_san_bai_shou<P: AsRef<Path>>(path: P) -> Result<Vec<TangShiSanBaiShou>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取文字蒙求json文件
fn read_user_from_file_wen_zi_meng_qiu<P: AsRef<Path>>(path: P) -> Result<Vec<WenZiMengQiu>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取幼学琼林json文件
fn read_user_from_file_you_xue_qiong_lin<P: AsRef<Path>>(path: P) -> Result<Vec<YouXueQiongLin>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取增广贤文json文件
fn read_user_from_file_zeng_guang_xian_wen<P: AsRef<Path>>(path: P) -> Result<Vec<ZengGuangXianWen>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取朱子家训json文件
fn read_user_from_file_zhu_zi_jia_xun<P: AsRef<Path>>(path: P) -> Result<Vec<ZhuZiJiaXun>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取诗经json文件
fn read_user_from_file_shi_jing<P: AsRef<Path>>(path: P) -> Result<Vec<ShiJing>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取大学json文件
fn read_user_from_file_da_xue<P: AsRef<Path>>(path: P) -> Result<Vec<DaXue>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}
// 读取花间集json文件
fn read_user_from_file_hua_jian_ji_preface<P: AsRef<Path>>(path: P) -> Result<Vec<HuaJiaJi>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}

fn read_user_from_file_you_meng_ying<P: AsRef<Path>>(path: P) -> Result<Vec<YouMengYing>, Box<dyn Error>> {
    // Open the file in read-only mode with buffer.
    let file = File::open(path)?;
    let reader = BufReader::new(file);

    // Read the JSON contents of the file as an instance of `User`.
    let u = serde_json::from_reader(reader)?;

    // Return the `User`.
    Ok(u)
}


// 存数据到mongodbs
fn mongodb_insert(docs:Vec<mongodb::bson::Document>,collection_name:String,update:bool){
    let client = Client::with_uri_str("mongodb://localhost:27017").unwrap();
    let database = client.database("chinese-poetry");
    let collection = database.collection::<Document>(&*collection_name);
    if update {
        collection.drop(None).unwrap();
    }
    let count = collection.insert_many(docs,None).unwrap();
    println!("{}集合更新数据{:?}条",collection.name().to_string(),count.inserted_ids.len());
}

fn cao_cao_shi_ji(){
    let data = read_user_from_file_poet("E:/wqm/project-code/github/chinese-poetry/caocaoshiji/caocao.json").unwrap();
    let mut docs = vec![];
    for poet in data {
        // Serialize it to a JSON string.
        let doc = doc!{"title":poet.title,"paragraphs":poet.paragraphs};
        docs.push(doc);
    }

    mongodb_insert(docs, "caocaoshiji".parse().unwrap(),true);
}

fn chu_ci(){
    let data = read_user_from_file_chu_ci("E:/wqm/project-code/github/chinese-poetry/chuci/chuci.json").unwrap();
    let mut docs = vec![];
    for chuci in data {
        // Serialize it to a JSON string.
        let doc = doc!{"title":chuci.title,"section":chuci.section,"author":chuci.author,"content":chuci.content};
        docs.push(doc);
    }

    mongodb_insert(docs, "chuci".parse().unwrap(),true);
}

fn song_ci_author(){
    let data = read_user_from_file_song_ci_author("E:/wqm/project-code/github/chinese-poetry/ci/author.song.json").unwrap();
    let mut docs = vec![];
    for songciauthor in data {
        // Serialize it to a JSON string.
        let doc = doc!{"name":songciauthor.name,"short_description":songciauthor.short_description,"description":songciauthor.description};
        docs.push(doc);
    }

    mongodb_insert(docs, "song-ci-author".parse().unwrap(),true);
}

fn song_ci(){
    for entry in WalkDir::new("E:/wqm/project-code/github/chinese-poetry/ci").into_iter().filter_map(|e| e.ok()) {
        let file_name = entry.file_name().to_str().unwrap();

        if file_name.contains("ci.song")
        {
            println!("{}", entry.path().display());
            let path = entry.path().display().to_string();
            let data = read_user_from_file_song_ci(path).unwrap();
            let mut docs = vec![];
            for songci in data {
                // Serialize it to a JSON string.
                let doc = doc! {"rhythmic":songci.rhythmic,"author":songci.author,"paragraphs":songci.paragraphs};
                docs.push( doc);
            }

            mongodb_insert(docs, "song-ci".parse().unwrap(), false);
        }
    }
}

fn poet_author(){
    let data = read_user_from_file_poet_author("E:/wqm/project-code/github/chinese-poetry/json/authors.tang.json").unwrap();
    let mut docs = vec![];
    for poetauthor in data {
        // Serialize it to a JSON string.
        let doc = doc!{"id":poetauthor.id,"name":poetauthor.name,"desc":poetauthor.desc};
        docs.push(doc);
    }

    mongodb_insert(docs, "tang-poet-author".parse().unwrap(),true);
}

fn poet_desc(){
    for entry in WalkDir::new("E:/wqm/project-code/github/chinese-poetry/json").into_iter().filter_map(|e| e.ok()) {
        let file_name = entry.file_name().to_str().unwrap();
        let path = entry.path().display().to_string();

        if file_name.contains("poet.tang") && !path.contains("error")
        {
            println!("{}", entry.path().display());
            let data = read_user_from_file_poet_desc(path).unwrap();
            let mut docs = vec![];
            for poetdesc in data {
                // Serialize it to a JSON string.
                let doc = doc!{"id":poetdesc.id,"title":poetdesc.title,"author":poetdesc.author,"paragraphs":poetdesc.paragraphs};
                docs.push(doc);
            }

            mongodb_insert(docs, "tang-poet".parse().unwrap(),false);
        }
    }
}

fn lun_yu(){
    let data = read_user_from_file_lun_yu("E:/wqm/project-code/github/chinese-poetry/lunyu/lunyu.json").unwrap();
    let mut docs = vec![];
    for lunyu in data {
        // Serialize it to a JSON string.
        let doc = doc!{"chapter":lunyu.chapter,"paragraphs":lunyu.paragraphs};
        docs.push(doc);
    }

    mongodb_insert(docs, "lun-yu".parse().unwrap(),true);
}

fn bai_jia_xing(){
    let data = read_user_from_file_bai_jia_xing("E:/wqm/project-code/github/chinese-poetry/mengxue/baijiaxing.json").unwrap();
    let mut docs = vec![];
    for baijiaxing in data {
        // Serialize it to a JSON string.
        let mut origin_list = vec![];
        for origin in baijiaxing.origin {
            let v = doc!{"surname":origin.surname,"place":origin.place};
            origin_list.push(v);
        }
        let doc = doc!{"title":baijiaxing.title,"author":baijiaxing.author,"tags":baijiaxing.tags,"paragraphs":baijiaxing.paragraphs,"origin":origin_list};
        //
        docs.push(doc);

    }

    mongodb_insert(docs, "bai_jia_xing".parse().unwrap(),true);
}

fn di_zi_gui(){
    let data = read_user_from_file_di_zi_gui("E:/wqm/project-code/github/chinese-poetry/mengxue/dizigui.json").unwrap();
    let mut docs = vec![];
    for dizigui in data {
        // Serialize it to a JSON string.
        let mut content_list = vec![];
        for content in dizigui.content {
            let v = doc!{"chapter":content.chapter,"paragraphs":content.paragraphs};
            content_list.push(v);
        }
        let doc = doc!{"title":dizigui.title,"author":dizigui.author,"content":content_list};
        //
        docs.push(doc);

    }

    mongodb_insert(docs, "di_zi_gui".parse().unwrap(),true);
}

fn gu_wen_guan_zhi(){
    let data = read_user_from_file_gu_wen_guan_zhi("E:/wqm/project-code/github/chinese-poetry/mengxue/guwenguanzhi.json").unwrap();
    let mut docs = vec![];
    for guwenguanzhi in data {
        // Serialize it to a JSON string.
        let mut juan_list = vec![];
        for juan in guwenguanzhi.content {
            let mut juan_desc_list = vec![];
            for juan_desc in juan.content {
                let juan_desc_value = doc!{"chapter":juan_desc.chapter,"source":juan_desc.source,"author":juan_desc.author,"paragraphs":juan_desc.paragraphs};
                juan_desc_list.push(juan_desc_value);
            }
            let juan_value = doc!{"title":juan.title,"content":juan_desc_list};
            juan_list.push(juan_value);
        }
        let doc = doc!{"title":guwenguanzhi.title,"brief":guwenguanzhi.brief,"content":juan_list};
        //
        docs.push(doc);

    }

    mongodb_insert(docs, "gu_wen_guan_zhi".parse().unwrap(),true);
}

fn qian_jia_shi(){
    let data = read_user_from_file_qian_jia_shi("E:/wqm/project-code/github/chinese-poetry/mengxue/qianjiashi.json").unwrap();
    let mut docs = vec![];
    for qianjiashi in data {
        // Serialize it to a JSON string.
        let mut type_list = vec![];
        for poet_type in qianjiashi.content {
            let mut qian_list = vec![];
            for qian_desc in poet_type.content {
                if qian_desc.paragraphs.is_empty() {
                    let mut subparagraph_list = vec![];
                    for subparagraph in qian_desc.subparagraphs {
                        let subparagraph_value = doc!{"subchapter":subparagraph.subchapter,"paragraphs":subparagraph.paragraphs};
                        subparagraph_list.push(subparagraph_value);
                    }
                    let qian_desc_value = doc!{"chapter":qian_desc.chapter,"author":qian_desc.author,"subparagraph":subparagraph_list};
                    qian_list.push(qian_desc_value);

                }else {
                    let qian_desc_value = doc!{"chapter":qian_desc.chapter,"author":qian_desc.author,"paragraphs":qian_desc.paragraphs};
                    qian_list.push(qian_desc_value);
                }

            }
            let type_value = doc!{"type":poet_type.poet_type,"content":qian_list};
            type_list.push(type_value);
        }
        let doc = doc!{"title":qianjiashi.title,"author":qianjiashi.author,"content":type_list};
        //
        docs.push(doc);

    }

    mongodb_insert(docs, "qian_jia_shi".parse().unwrap(),true);
}

fn qian_zi_wen(){
    let data = read_user_from_file_qian_zi_wen("E:/wqm/project-code/github/chinese-poetry/mengxue/qianziwen.json").unwrap();
    let mut docs = vec![];
    for qianziwen in data {
        // Serialize it to a JSON string.
        let doc = doc!{"title":qianziwen.title,"author":qianziwen.author,"tags":qianziwen.tags,"paragraphs":qianziwen.paragraphs,"spells":qianziwen.spells};
        docs.push(doc);
    }

    mongodb_insert(docs, "qian_zi_wen".parse().unwrap(),true);
}

fn san_zi_jing(){
    let data = read_user_from_file_san_zi_jing("E:/wqm/project-code/github/chinese-poetry/mengxue/sanzijing-new.json").unwrap();
    let mut docs = vec![];
    for sanzijing in data {
        // Serialize it to a JSON string.
        let doc = doc!{"title":sanzijing.title,"author":sanzijing.author,"tags":sanzijing.tags,"paragraphs":sanzijing.paragraphs};
        docs.push(doc);
    }

    mongodb_insert(docs, "san_zi_jing".parse().unwrap(),false);
}

fn sheng_lv_qi_meng(){
    let data = read_user_from_file_sheng_lv_qi_meng("E:/wqm/project-code/github/chinese-poetry/mengxue/shenglvqimeng.json").unwrap();
    let mut docs = vec![];
    for shenglvqimeng in data {
        // Serialize it to a JSON string.
        let mut juan_list = vec![];
        for juan in shenglvqimeng.content {
            let mut juan_desc_list = vec![];
            for juan_desc in juan.content {
                let juan_desc_value = doc!{"chapter":juan_desc.chapter,"paragraphs":juan_desc.paragraphs};
                juan_desc_list.push(juan_desc_value);
            }
            let juan_value = doc!{"title":juan.title,"content":juan_desc_list};
            juan_list.push(juan_value);
        }
        let doc = doc!{"title":shenglvqimeng.title,"author":shenglvqimeng.author,"brief":shenglvqimeng.brief,"content":juan_list};
        //
        docs.push(doc);

    }

    mongodb_insert(docs, "sheng_lv_qi_meng".parse().unwrap(),true);
}

fn tang_shi_san_bai_shou(){
    let data = read_user_from_file_tang_shi_san_bai_shou("E:/wqm/project-code/github/chinese-poetry/mengxue/tangshisanbaishou.json").unwrap();
    let mut docs = vec![];
    for tangshisanbaishou in data {
        // Serialize it to a JSON string.
        let mut type_list = vec![];
        for poet_type in tangshisanbaishou.content {
            let mut poet_desc_list = vec![];
            for poet_desc in poet_type.content {
                let poet_desc_value = doc!{"chapter":poet_desc.chapter,"subchapter":poet_desc.subchapter,"author":poet_desc.author,"paragraphs":poet_desc.paragraphs};
                poet_desc_list.push(poet_desc_value);
            }
            let poet_type_value = doc!{"type":poet_type.poet_type,"content":poet_desc_list};
            type_list.push(poet_type_value);
        }
        let doc = doc!{"title":tangshisanbaishou.title,"content":type_list};
        //
        docs.push(doc);

    }

    mongodb_insert(docs, "tang_shi_san_bai_shou".parse().unwrap(),true);
}

fn wen_zi_meng_qiu(){
    let data = read_user_from_file_wen_zi_meng_qiu("E:/wqm/project-code/github/chinese-poetry/mengxue/wenzimengqiu.json").unwrap();
    let mut docs = vec![];
    for wenzimengqiu in data {
        // Serialize it to a JSON string.
        let mut juan_list = vec![];
        for juan in wenzimengqiu.content {
            let juan_list_value = doc!{"title":juan.title,"paragraphs":juan.paragraphs};
            juan_list.push(juan_list_value);
        }
        let doc = doc!{"title":wenzimengqiu.title,"author":wenzimengqiu.author,"brief":wenzimengqiu.brief,"preface":wenzimengqiu.preface,"content":juan_list};
        //
        docs.push(doc);

    }

    mongodb_insert(docs, "wen_zi_meng_qiu".parse().unwrap(),true);
}

fn you_xue_qiong_lin(){
    let data = read_user_from_file_you_xue_qiong_lin("E:/wqm/project-code/github/chinese-poetry/mengxue/youxueqionglin.json").unwrap();
    let mut docs = vec![];
    for youxueqionglin in data {
        // Serialize it to a JSON string.
        let mut juan_list = vec![];
        for juan in youxueqionglin.content {
            let mut juan_desc_list = vec![];
            for juan_desc in juan.content {
                let juan_desc_value = doc!{"chapter":juan_desc.chapter,"paragraphs":juan_desc.paragraphs};
                juan_desc_list.push(juan_desc_value);
            }
            let juan_value = doc!{"title":juan.title,"content":juan_desc_list};
            juan_list.push(juan_value);
        }
        let doc = doc!{"title":youxueqionglin.title,"author":youxueqionglin.author,"brief":youxueqionglin.brief,"content":juan_list};
        //
        docs.push(doc);

    }

    mongodb_insert(docs, "you_xue_qiong_lin".parse().unwrap(),true);
}

fn zeng_guang_xian_wen(){
    let data = read_user_from_file_zeng_guang_xian_wen("E:/wqm/project-code/github/chinese-poetry/mengxue/zengguangxianwen.json").unwrap();
    let mut docs = vec![];
    for zengguangxianwen in data {
        // Serialize it to a JSON string.
        let mut ji_list = vec![];
        for ji in zengguangxianwen.content {
            let ji_value = doc!{"chapter":ji.chapter,"paragraphs":ji.paragraphs};
            ji_list.push(ji_value);
        }
        let doc = doc!{"title":zengguangxianwen.title,"author":zengguangxianwen.author,"brief":zengguangxianwen.brief,"content":ji_list};
        //
        docs.push(doc);

    }

    mongodb_insert(docs, "zeng_guang_xian_wen".parse().unwrap(),true);
}

fn zhu_zi_jia_xun(){
    let data = read_user_from_file_zhu_zi_jia_xun("E:/wqm/project-code/github/chinese-poetry/mengxue/zhuzijiaxun.json").unwrap();
    let mut docs = vec![];
    for zhuzijiaxun in data {
        // Serialize it to a JSON string.
        let doc = doc!{"title":zhuzijiaxun.title,"author":zhuzijiaxun.author,"paragraphs":zhuzijiaxun.paragraphs};
        docs.push(doc);
    }

    mongodb_insert(docs, "zhu_zi_jia_xun".parse().unwrap(),true);
}

fn shi_jing(){
    let data = read_user_from_file_shi_jing("E:/wqm/project-code/github/chinese-poetry/shijing/shijing.json").unwrap();
    let mut docs = vec![];
    for shijing in data {
        // Serialize it to a JSON string.
        let doc = doc!{"title":shijing.title,"chapter":shijing.chapter,"section":shijing.section,"content":shijing.content};
        docs.push(doc);
    }

    mongodb_insert(docs, "shi_jing".parse().unwrap(),true);
}

fn da_xue(){
    let data = read_user_from_file_da_xue("E:/wqm/project-code/github/chinese-poetry/sishuwujing/daxue.json").unwrap();
    let mut docs = vec![];
    for daxue in data {
        // Serialize it to a JSON string.
        let doc = doc!{"chapter":daxue.chapter,"paragraphs":daxue.paragraphs};
        docs.push(doc);
    }

    mongodb_insert(docs, "da_xue".parse().unwrap(),true);
}
fn meng_zi(){
    let data = read_user_from_file_da_xue("E:/wqm/project-code/github/chinese-poetry/sishuwujing/mengzi.json").unwrap();
    let mut docs = vec![];
    for mengzi in data {
        // Serialize it to a JSON string.
        let doc = doc!{"chapter":mengzi.chapter,"paragraphs":mengzi.paragraphs};
        docs.push(doc);
    }

    mongodb_insert(docs, "meng_zi".parse().unwrap(),true);
}
fn zhong_yong(){
    let data = read_user_from_file_da_xue("E:/wqm/project-code/github/chinese-poetry/sishuwujing/zhongyong.json").unwrap();
    let mut docs = vec![];
    for zhongyong in data {
        // Serialize it to a JSON string.
        let doc = doc!{"chapter":zhongyong.chapter,"paragraphs":zhongyong.paragraphs};
        docs.push(doc);
    }

    mongodb_insert(docs, "zhong_yong".parse().unwrap(),true);
}

fn poet_strains(){
    for entry in WalkDir::new("E:/wqm/project-code/github/chinese-poetry/strains/json").into_iter().filter_map(|e| e.ok()) {
        let file_name = entry.file_name().to_str().unwrap();
        let path = entry.path().display().to_string();

        if file_name.contains("poet.tang")
        {
            println!("{}", entry.path().display());
            let data = read_user_from_file_poet_strains(path).unwrap();
            let mut docs = vec![];
            for poet_strains in data {
                // Serialize it to a JSON string.
                let doc = doc!{"id":poet_strains.id,"strains":poet_strains.strains};
                docs.push(doc);
            }

            mongodb_insert(docs, "tang-poet-strains".parse().unwrap(),false);
        }
    }
}

fn hua_jian_ji_preface(){
    let data = read_user_from_file_hua_jian_ji_preface("E:/wqm/project-code/github/chinese-poetry/wudai/huajianji/huajianji-0-preface.json").unwrap();
    let mut docs = vec![];
    for huajianjipreface in data {
        // Serialize it to a JSON string.
        let doc = doc!{"title":huajianjipreface.title,"author":huajianjipreface.author,"rhythmic":huajianjipreface.rhythmic,"paragraphs":huajianjipreface.paragraphs,"notes":huajianjipreface.notes};
        docs.push(doc);
    }

    mongodb_insert(docs, "hua_jian_ji_preface".parse().unwrap(),true);
}

fn hua_jian_ji_juan(){
    for entry in WalkDir::new("E:/wqm/project-code/github/chinese-poetry/wudai/huajianji").into_iter().filter_map(|e| e.ok()) {
        let file_name = entry.file_name().to_str().unwrap();
        let path = entry.path().display().to_string();

        if file_name.contains("juan")
        {
            println!("{}", entry.path().display());
            let data = read_user_from_file_hua_jian_ji_preface(path).unwrap();
            let mut docs = vec![];
            for huajianjipreface in data {
                // Serialize it to a JSON string.
                let doc = doc!{"title":huajianjipreface.title,"author":huajianjipreface.author,"rhythmic":huajianjipreface.rhythmic,"paragraphs":huajianjipreface.paragraphs,"notes":huajianjipreface.notes};
                docs.push(doc);
            }

            mongodb_insert(docs, "hua_jian_ji_juan".parse().unwrap(),false);
        }
    }

}

fn nan_tang_er_zhu_ci(){
    let data = read_user_from_file_hua_jian_ji_preface("E:/wqm/project-code/github/chinese-poetry/wudai/nantang/poetrys.json").unwrap();
    let mut docs = vec![];
    for huajianjipreface in data {
        // Serialize it to a JSON string.
        let doc = doc!{"title":huajianjipreface.title,"author":huajianjipreface.author,"rhythmic":huajianjipreface.rhythmic,"paragraphs":huajianjipreface.paragraphs,"notes":huajianjipreface.notes};
        docs.push(doc);
    }

    mongodb_insert(docs, "nan_tang_er_zhu_ci".parse().unwrap(),true);
}

fn you_meng_ying(){
    let data = read_user_from_file_you_meng_ying("E:/wqm/project-code/github/chinese-poetry/youmengying/youmengying.json").unwrap();
    let mut docs = vec![];
    for youmengying in data {
        // Serialize it to a JSON string.
        let doc = doc!{"content":youmengying.content,"comment":youmengying.comment};
        docs.push(doc);
    }

    mongodb_insert(docs, "you_meng_ying".parse().unwrap(),true);
}

fn main() {
    //cao_cao_shi_ji();
    //chu_ci();
    //song_ci_author();
    //song_ci();
    //poet_author();
    //poet_desc();
    //lun_yu();
    //bai_jia_xing();
    //di_zi_gui();
    //gu_wen_guan_zhi();
    //qian_jia_shi();
    //qian_zi_wen();
    //san_zi_jing();
    //sheng_lv_qi_meng();
    //tang_shi_san_bai_shou();
    //wen_zi_meng_qiu();
    //you_xue_qiong_lin();
    //shi_jing();
    //da_xue();
    //meng_zi();
    //zhong_yong();
    //poet_strains();
    //hua_jian_ji_preface();
    //hua_jian_ji_juan();
    //nan_tang_er_zhu_ci();
    //you_meng_ying();



}